﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using OL.FrameCore.Application;
using OL.FrameCore.Application.CommonData;
using OL.FrameCore.Application.Dept;
using OL.FrameCore.Application.User;
using OL.FrameCore.Application.User.Dto;
using OL.FrameCore.Infrastructure.Document;
using OL.FrameCore.WebUI.ViewModels.User;

namespace OL.FrameCore.WebUI.Controllers
{
    public class UserController : ControllerBase
    {
        IUserService _userService;
        ICommonDataService _commonDataService;

        public UserController(IUserService userService, ICommonDataService commonDataService)
        {
            _userService = userService;
            _commonDataService = commonDataService;
        }

        public IActionResult Index()
        {
            ViewBag.DeptList = _commonDataService.GetDeptDropDownList();
            return View();
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IActionResult GetUserList(ViewModels.User.GetUserListViewRequest request)
        {
            var serviceRequest = ToMap<GetUserListViewRequest, GetUserListRequest>(request);
            serviceRequest.PageIndex = request.page - 1;
            serviceRequest.PageSize = request.limit;
            var serviceResult = _userService.GetUserList(serviceRequest);
            var result = new
            {
                code = 0,
                msg = "",
                count = serviceResult.ItemsCount,
                data = serviceResult.Items
            };

            return Json(result);
        }
       
        /// <summary>
        /// 添加用户页面
        /// </summary>
        /// <returns></returns>
        public IActionResult AddUser()
        {
            ViewBag.DeptList = _commonDataService.GetDeptDropDownList();
            return View();
        }
        /// <summary>
        /// 提交添加用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostAddUser(PostAddUserViewRequest request)
        {
            //验证模型数据
            if (IsNotValidModel)
            {
                return FailResult(ModelErrorMessage);
            }

            var serviceRequest = ToMap<PostAddUserViewRequest, AddUserRequest>(request);
            serviceRequest.OpeatorId = this.UserId;
            var result = _userService.AddUser(serviceRequest);
            return Json(result);
        }

        /// <summary>
        /// 修改用户页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult UpdateUser(int id)
        {
            ViewBag.DeptList = _commonDataService.GetDeptDropDownList();
            GetUserResponse model = _userService.GetUser(id);
            return View(model);
        }
        /// <summary>
        /// 提交修改用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostUpdateUser(PostUpdateUserViewRequest request)
        {
            //验证模型数据
            if (IsNotValidModel)
            {
                return FailResult(ModelErrorMessage);
            }

            var serviceRequest = ToMap<PostUpdateUserViewRequest, UpdateUserRequest>(request);
            serviceRequest.OpeatorId = this.UserId;
            var result = _userService.UpdateUser(serviceRequest);
            return Json(result);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteUser(int id)
        {
            var result = _userService.DeleteUser(id, UserId);
            return Json(result);
        }

        /// <summary>
        /// 启用用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult EnableUser(int id)
        {
            var result = _userService.EnableUser(id, UserId);
            return Json(result);
        }
        /// <summary>
        /// 禁用用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DisableUser(int id)
        {
            var result = _userService.DisableUser(id, UserId);
            return Json(result);
        }

        /// <summary>
        /// 重置密码页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult RetsetPassword(int id)
        {
            ViewBag.UserId = id;
            return View();
        }
        /// <summary>
        /// 提交重置密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostRetsetPassword(RetsetPasswordViewRequest request)
        {
            //验证模型数据
            if (IsNotValidModel)
            {
                return FailResult(ModelErrorMessage);
            }

            var serviceRequest = ToMap<RetsetPasswordViewRequest, RetsetPasswordRequest>(request);
            serviceRequest.OpeatorId = UserId;
            var result = _userService.RetsetPassword(serviceRequest);
            return Json(result);
        }

        /// <summary>
        /// 分配角色页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult AssignUserRole(int id)
        {
            var model = _userService.GetUserRole(id);
            return View(model);
        }
        /// <summary>
        /// 提交分配角色
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostAssignUserRole(PostAssignUserRoleViewRequest request)
        {
            var serviceRequest = ToMap<PostAssignUserRoleViewRequest, AssignUserRoleRequest>(request);
            serviceRequest.OpeatorId = UserId;
            var result = _userService.AssignUserRole(serviceRequest);
            return Json(result);
        }
        /// <summary>
        /// 分配权限页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult AssignUserModule(int id)
        {
            var model = _userService.GetUserModule(id);
            return View(model);
        }
        /// <summary>
        /// 提交分配权限
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostAssignUserModule(PostAssignUserModuleViewRequest request)
        {
            var serviceRequest = ToMap<PostAssignUserModuleViewRequest, AssignUserModuleRequest>(request);
            serviceRequest.OpeatorId = UserId;
            var result = _userService.AssignUserModule(serviceRequest);
            return Json(result);
        }


        //public ActionResult Upload()
        //{
        //    var file = Request.Form.Files.FirstOrDefault();
        //    var stream = file.OpenReadStream();

        //    string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "File", file.FileName);
        //    using (FileStream fileStream = System.IO.File.Create(filePath))
        //    {
        //        file.CopyTo(fileStream);
        //        fileStream.Flush();
        //    }

        //    return Json(new { code = 0 });
        //}

        //public ActionResult Down()
        //{
        //    string fileName = "项目依赖包说明.xlsx";
        //    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "File", fileName);
        //    FileInfo fileInfo = new FileInfo(path);
        //    if (fileInfo.Exists == false)
        //    {
        //        return new ContentResult { StatusCode = 404 };
        //    }
        //    var stream = fileInfo.OpenRead();
        //    var provider = new FileExtensionContentTypeProvider();
        //    var memi = provider.Mappings[fileInfo.Extension];
        //    return File(stream, memi, fileName, true);

        //}

        public FileResult ExportUser()
        {
            string fileName = "项目依赖包说明.xlsx";


            var serviceResult = _userService.GetUserList(new GetUserListRequest
            {
                PageIndex = 0,
                PageSize = 10
            });

            NPOIExcel excel = new NPOIExcel(fileName, serviceResult.Items);
            var stream = excel.Create();
            var provider = new FileExtensionContentTypeProvider();
            var memi = provider.Mappings[".xls"];
            return File(stream, memi, fileName);



            //string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "File", fileName);
            //FileInfo fileInfo = new FileInfo(path);
            //if (fileInfo.Exists == false)
            //{
            //    return new ContentResult { StatusCode = 404 };
            //}
            //var stream = fileInfo.OpenRead();
            //var provider = new FileExtensionContentTypeProvider();
            //var memi = provider.Mappings[fileInfo.Extension];
            //return File(stream, memi, fileName, true);

        }
    }
}